﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonFramework.Utility;
using Xinkui.DataAccess.AutoGenerator;
using Xinkui.Common;
using Xinkui.WebModel.Helper;

public partial class UserControl_UserCenter_UC_RecycleBox : Xinkui.WebModel.BasicControl.AccountControl {

    protected int RowLine {
        get {
            return WebHelper.GetQueryStringValue<int>("line", 1);
        }
    }

    protected int PageIndex {
        get {
            return WebHelper.GetQueryStringValue<int>("p", 1);
        }
    }

    protected string BaseURL { get; set; }


    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {

            this.DataBind();

        }
    }


    public override void DataBind() {
        base.DataBind();
        int pageIndex = PageIndex;
        if (pageIndex <= 0) {
            pageIndex = 1;
        }

        int pageSize = (RowLine > 3 ? 3 : (RowLine < 1 ? 1 : RowLine)) * 10;
        if (pageSize < 10) {
            pageSize = 10;
        }
        int total = 0;
        var listMessage = Singleton<MessageInBoxDataAccess>.Instance.ListRecyleBox(pageSize, pageIndex, this.BasePage.CurrentLogin.UserID, out total);




        UC_Pager1.FormatUrl = true;
        UC_Pager1.CoverNav = false;
        this.UC_Pager1.JumpUrl = BuildUrl("p", "{0}");//"ProductListForImage.aspx?com=1&p={0}";//JumpUrl;
        this.UC_Pager1.PageIndex = pageIndex;
        this.UC_Pager1.PageSize = pageSize;
        this.UC_Pager1.PageStep = 4;
        UC_Pager1.NoteTotal = total;


        if (listMessage != null && listMessage.Count > 0) {
            this.rptDataSource.DataSource = listMessage;
            this.rptDataSource.DataBind();
            this.ltNoData.Visible = false;
        }
        else {
            this.ltNoData.Visible = true;
        }

        BaseURL = BuildUrl("line", "");
        UC_Pager1.Show();
    }



    public void btnDel_Click(object sender, EventArgs e) {

        string[] chkeckMessage = WebHelper.GetFormValue<string>("chkSelect", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

        if (chkeckMessage != null && chkeckMessage.Length > 0) {
            foreach (string idx in chkeckMessage) {
                Singleton<MessageInBoxDataAccess>.Instance.ClearMessage(this.CurrentLogin.UserID, ConvertHelper.ToType<int>(idx, 0));
            }
        }
        UIHelper.ShowSuucessTips("Record has cleared.");
        this.DataBind();
    }

    public void btnRefresh_Click(object sender, EventArgs e) {
        this.DataBind();
    }



    public string BuildUrl(string key, object val) {
        List<string> querys = new List<string>();
        bool matched = false;
        foreach (string k in Request.QueryString.AllKeys) {
            if (k.Equals(key)) {
                querys.Add(string.Format("{0}={1}", k, val));
                matched = true;
            }
            else {
                querys.Add(string.Format("{0}={1}", k, Request.QueryString[k]));
            }
        }
        if (!matched) {
            querys.Add(string.Format("{0}={1}", key, val));
        }

        return string.Format("Inbox.aspx?{0}", string.Join("&", querys));
    }
}